package com.limao.DBProvider;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import com.limao.DBDataClass.SysRmbSelectdataClass;
import com.limao.DbConnection.DbConnectionManager;
import com.limao.Tool.CacheClass;

public class SysRmbSelectProvider {
	
	private static SysRmbSelectProvider instance;

	public static SysRmbSelectProvider getInstance() {
		if(instance==null) instance=new SysRmbSelectProvider();
		return instance;

	}

	//全部
	public ArrayList<SysRmbSelectdataClass> load_sys_RmbSelect_all() {
		ArrayList<SysRmbSelectdataClass> tmpInfo = new ArrayList<SysRmbSelectdataClass>();
		SysRmbSelectdataClass tmp = null;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("select * from cd_sys_rmbselect");
			rs = pstmt.executeQuery();
			while (rs.next()) {
				tmp = new SysRmbSelectdataClass();
				tmp.id = rs.getInt("id") + "";
				tmp.rmb = rs.getInt("rmb")+"";
				tmp.taxrate = rs.getInt("taxrate")+"";
				tmp.state = rs.getInt("state")+"";
				tmpInfo.add(tmp);
			}

		} catch (Exception e) {
			tmpInfo .clear();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return tmpInfo;
	}
	
	//所有启用的
	public ArrayList<SysRmbSelectdataClass> load_sys_RmbSelect_state1() {
		ArrayList<SysRmbSelectdataClass> tmpInfo = new ArrayList<SysRmbSelectdataClass>();
		SysRmbSelectdataClass tmp = null;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("select * from cd_sys_rmbselect where state=1 order by rmb asc");
			rs = pstmt.executeQuery();
			while (rs.next()) {
				tmp = new SysRmbSelectdataClass();
				tmp.id = rs.getInt("id") + "";
				tmp.rmb = rs.getInt("rmb")+"";
				tmp.taxrate = rs.getInt("taxrate")+"";
				tmp.state = rs.getInt("state")+"";
				tmpInfo.add(tmp);
			}

		} catch (Exception e) {
			tmpInfo .clear();
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return tmpInfo;
	}
		
	

	public SysRmbSelectdataClass Load_SysRmbSelectBy_ID(String id) {

		SysRmbSelectdataClass tmp = null;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("select * from cd_sys_rmbselect where id=?");
			pstmt.setInt(1, CacheClass.getIntValue(id));
			rs = pstmt.executeQuery();
			if (rs.next()) {
				tmp = new SysRmbSelectdataClass();
				tmp.id = rs.getInt("id") + "";
				tmp.rmb = rs.getInt("rmb")+"";
				tmp.taxrate = rs.getInt("taxrate")+"";
				tmp.state = rs.getInt("state")+"";
			}

		} catch (Exception e) {
			tmp = null;
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return tmp;
	}
	
	public SysRmbSelectdataClass Update_SysRmbSelectBy_ID(int id,int rmb ,int state) {

		SysRmbSelectdataClass tmp = null;
		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("update cd_sys_rmbselect set rmb=?,state=? where id=?");
			pstmt.setInt(1, rmb);
			pstmt.setInt(2, state);
			pstmt.setInt(3, id);
			pstmt.executeUpdate();
		} catch (Exception e) {
			tmp = null;
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return tmp;
	}	
	
	public boolean Add_SysRmbSelectBy_ID(int rmb ,int state) {

		Connection con = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			con = DbConnectionManager.getConnection();
			pstmt = con.prepareStatement("INSERT into cd_sys_rmbselect (rmb,state) VALUES (?,?)");
			pstmt.setInt(1, rmb);
			pstmt.setInt(2, state);
			pstmt.execute();
		} catch (Exception e) {
			return false;
		} finally {
			DbConnectionManager.closeConnection(rs, pstmt, con);
		}
		return true;
	}		
	
}
